package com.qfedu;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import java.io.File;

/**
 * @ClassName SpringBootApp
 * @Author ykx
 * @Date 2024/5/20 17:08
 * @Version 1.0
 */
public class SpringBootApp {
    public static void main(String[] args) throws LifecycleException {
        start();
    }

    public static void start() throws LifecycleException {
        System.out.println("开始启动tomcat");
        // 创建tomcat服务
        Tomcat tomcat = new Tomcat();

        // 添加webapp
        Context context = tomcat.addWebapp("", new File("src/main/java").getAbsolutePath());
        // webapp禁止重载入
        context.setReloadable(false);
        // 加载classes文件
        File file = new File("target/classes");
        // 创建web应用资源
        StandardRoot standardRoot = new StandardRoot(context);
        standardRoot.addPreResources(new DirResourceSet(standardRoot, "/WEB-INF/classes", file.getAbsolutePath(), "/"));

        tomcat.getConnector();
        // 启动tomcat
        tomcat.start();
        System.out.println("启动tomcat完毕");
        // 开启异步接收http请求接入
        tomcat.getServer().await();
    }

}
